package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.TeacherIntentionVO;
import edu.zqu.instructionalMatters.project.entity.TeacherIntention;
import edu.zqu.instructionalMatters.project.mapper.TeacherIntentionMapper;
import edu.zqu.instructionalMatters.project.util.TeacherIntentionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 教师授课意向 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/teacherIntention")
@Api(tags = "教师授课意向")
public class TeacherIntentionController {
    @Autowired
    private TeacherIntentionMapper teacherIntentionMapper;
    @Autowired
    private TeacherIntentionUtil teacherIntentionUtil;

    @ApiOperation(value = "获取教师授课意向列表")
    @RequestMapping(value = "/teacherIntentions", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false),
            @ApiImplicitParam(name = "classCurriculumId", value = "班级课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = false),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger teacherId,
                             BigInteger classCurriculumId,
                             BigInteger courseId,
                             BigInteger semesterId,
                             BigInteger instructionClassId
    ) {
        TeacherIntention teacherIntention1 = new TeacherIntention()
                .setCollegeId(collegeId).setTeacherId(teacherId).setClassCurriculumId(classCurriculumId)
                .setCourseId(courseId).setSemesterId(semesterId).setInstructionClassId(instructionClassId);             //合成TeacherIntention对象
        List<TeacherIntention> list1 = teacherIntentionMapper.findAll(teacherIntention1);                               //获取TeacherIntention集合
        List<TeacherIntentionVO> list = new ArrayList<>();                                                              //初始化TeacherIntentionVO 集合
        for (TeacherIntention teacherIntention : list1) {
            TeacherIntentionVO teacherIntentionVO = teacherIntentionUtil.teacherIntentionUtil(teacherIntention);        //调用工具类赋值
            list.add(teacherIntentionVO);                                                                               //加入到 list
        }
        return ZQUResult.success(list);                                                                                 //返回 list
    }

    @ApiOperation(value = "获取教师授课意向信息", response = TeacherIntentionVO.class)
    @GetMapping(value = "/teacherIntention/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        TeacherIntention teacherIntention = teacherIntentionMapper.selectById(id);                          //查询对象数据
        TeacherIntentionVO teacherIntentionVO = teacherIntentionUtil.teacherIntentionUtil(teacherIntention);//设置教师名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", teacherIntentionVO);                      //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/teacherIntention", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false),
            @ApiImplicitParam(name = "classCurriculumId", value = "班级课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = false),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger teacherId,
                         BigInteger classCurriculumId,
                         BigInteger courseId,
                         BigInteger semesterId,
                         BigInteger instructionClassId,
                         BigInteger byWho) {
        TeacherIntention teacherIntention = new TeacherIntention();                    //初始化
        teacherIntention.setCollegeId(collegeId);                                      //设置学校id
        teacherIntention.setTeacherId(teacherId);                                      //设置教师id
        teacherIntention.setClassCurriculumId(classCurriculumId);                      //设置班级课程id
        teacherIntention.setCourseId(courseId);                                        //设置课程id
        teacherIntention.setSemesterId(semesterId);                                    //设置学期id
        teacherIntention.setInstructionClassId(instructionClassId);                    //设置授课班级id
        teacherIntention.setByWho(byWho);                                              //设置操作者
        teacherIntentionMapper.insert(teacherIntention);                               //执行插入
        return ZQUResult.success();                                                    //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/teacherIntention/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false),
            @ApiImplicitParam(name = "classCurriculumId", value = "班级课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = false),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger teacherId,
                            BigInteger classCurriculumId,
                            BigInteger courseId,
                            BigInteger semesterId,
                            BigInteger instructionClassId,
                            BigInteger byWho) {
        TeacherIntention teacherIntention = teacherIntentionMapper.selectById(id);     //先查出旧纪录
        teacherIntention.setByWho(byWho);                                              //修改操作者
        teacherIntentionMapper.updateById(teacherIntention);                           //更新操作者
        teacherIntentionMapper.deleteById(id);                                         //逻辑删除
        teacherIntention = new TeacherIntention();                                     //初始化新记录
        teacherIntention.setCollegeId(collegeId);                                      //设置学校id
        teacherIntention.setTeacherId(teacherId);                                      //设置教师id
        teacherIntention.setClassCurriculumId(classCurriculumId);                      //设置班级课程id
        teacherIntention.setCourseId(courseId);                                        //设置课程id
        teacherIntention.setSemesterId(semesterId);                                    //设置学期id
        teacherIntention.setInstructionClassId(instructionClassId);                    //设置授课班级id
        teacherIntention.setByWho(byWho);                                              //设置操作者
        teacherIntention.setLastVersion(id);                                           //设置上一版本
        teacherIntentionMapper.insert(teacherIntention);                               //执行插入
        return ZQUResult.success();                                                    //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/teacherIntention/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        TeacherIntention teacherIntention = teacherIntentionMapper.selectById(id);     //先查出旧纪录
        teacherIntention.setByWho(byWho);                                              //修改操作者
        teacherIntentionMapper.updateById(teacherIntention);                           //更新操作者
        teacherIntentionMapper.deleteById(id);                                         //逻辑删除
        return ZQUResult.success();                                                    //操作成功返回
    }
}

